package com.dharma.mydiet.storage;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DAOFood {

	public final static String KEY_ID = "idFood";
	public final static String KEY_NAME = "name";
	public final static String KEY_CALORIES = "calories";
	public final static String KEY_PORC_CARBOHIDRATOS = "porcCarbohidrats";
	public final static String KEY_PORC_LIPIDS = "porcLipids";
	public final static String KEY_PORC_PROTEINS = "porcProteins";
	public final static String KEY_SUGAR = "sugar";
	public final static String KEY_COLESTEROL = "colesterol";
	public final static String DATABASE_TABLE = "food";
	private static DAOFood instance;
	SQLiteDatabase myDataBase;

	private static final String[] cols = new String[] { KEY_ID, KEY_NAME,
			KEY_CALORIES, KEY_PORC_CARBOHIDRATOS, KEY_PORC_LIPIDS,
			KEY_PORC_PROTEINS, KEY_SUGAR, KEY_COLESTEROL };

	public static DAOFood getInstance (){
		if (instance == null){
			instance = new DAOFood();
		}
		return instance;
	}
	
	private DAOFood() {
		myDataBase = DBManager.getInstance().getMyDataBase();
	}

	public void insertFood(TransactionalFood food) {
		ContentValues newValues = new ContentValues();
		//newValues.put(KEY_ID, food.getIdFood());
		newValues.put(KEY_NAME, food.getName());
		newValues.put(KEY_CALORIES, food.getCalories());
		newValues.put(KEY_PORC_CARBOHIDRATOS, food.getPorcentualCarbohidraths());
		newValues.put(KEY_PORC_LIPIDS, food.getPorcentualLipids());
		newValues.put(KEY_PORC_PROTEINS, food.getPorcentualProteins());
		newValues.put(KEY_SUGAR, food.getShugar());
		newValues.put(KEY_COLESTEROL, food.getColesterol());
		myDataBase.beginTransaction();
		myDataBase.insert(DATABASE_TABLE, null, newValues);
		myDataBase.setTransactionSuccessful();
		myDataBase.endTransaction();
	}

	public boolean removeDevice(TransactionalFood device) {

		boolean ret = false;
		int row = this.getRow(String.valueOf(device.getIdFood()));

		if (row >= 0) {
			ret = removeDevice(row);
		}
		return ret;
	}

	private int getRow(String idfood) {
		int row = -1;
		try {
			Cursor result = null;
			result = myDataBase.query(true, DATABASE_TABLE, cols, KEY_ID + "='"
					+ idfood + "'", null, null, null, null, null);

			if (!((result.getCount() == 0) || !result.moveToFirst())) {
				if (result.moveToFirst()) {
					row = result.getInt(result.getColumnIndex(KEY_ID));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return row;
	}

	public boolean removeDevice(long _rowIndex) {
		myDataBase.beginTransaction();
		boolean result = myDataBase.delete(DATABASE_TABLE, KEY_ID + "="
				+ _rowIndex, null) > 0;
		myDataBase.setTransactionSuccessful();
		myDataBase.endTransaction();
		return result;

	}

	public boolean updateDevice(TransactionalFood food) {
		int row = this.getRow(String.valueOf(food.getIdFood()));
		return updateDevice(row, food);
	}

	public boolean updateDevice(Integer _rowIndex, TransactionalFood food) {
		ContentValues newValues = new ContentValues();
		newValues.put(KEY_ID, food.getIdFood());
		newValues.put(KEY_NAME, food.getName());
		newValues.put(KEY_CALORIES, food.getCalories());
		newValues
				.put(KEY_PORC_CARBOHIDRATOS, food.getPorcentualCarbohidraths());
		newValues.put(KEY_PORC_LIPIDS, food.getPorcentualLipids());
		newValues.put(KEY_PORC_PROTEINS, food.getPorcentualProteins());
		newValues.put(KEY_SUGAR, food.getShugar());
		newValues.put(KEY_COLESTEROL, food.getColesterol());
		return myDataBase.update(DATABASE_TABLE, newValues, KEY_ID + "="
				+ _rowIndex, null) > 0;
	}

	public TransactionalFood getFood(String foodId) {
		TransactionalFood food = null;
		Cursor result = myDataBase.query(true, DATABASE_TABLE, cols, KEY_ID
				+ "= '" + foodId + "'", null, null, null, null, null);
		if (!((result.getCount() == 0) || !result.moveToFirst())) {
			if (result.moveToFirst()) {
				food = new TransactionalFood(
						result.getInt(result.getColumnIndex(KEY_ID)),
						result.getString(result.getColumnIndex(KEY_NAME)),
						result.getInt(result.getColumnIndex(KEY_CALORIES)),
						result.getFloat(result
								.getColumnIndex(KEY_PORC_CARBOHIDRATOS)),
						result.getFloat(result.getColumnIndex(KEY_PORC_LIPIDS)),
						result.getFloat(result
								.getColumnIndex(KEY_PORC_PROTEINS)), result
								.getInt(result.getColumnIndex(KEY_SUGAR)),
						result.getFloat(result.getColumnIndex(KEY_COLESTEROL)));

			}
		}
		return food;
	}

	public ArrayList<TransactionalFood> getFoods() {
		ArrayList<TransactionalFood> devices = new ArrayList<TransactionalFood>();
		Cursor result = myDataBase.query(DATABASE_TABLE, cols, null, null,
				null, null, KEY_ID);
		if (result.moveToFirst())
			do {
				devices.add(new TransactionalFood(result.getInt(result
						.getColumnIndex(KEY_ID)), result.getString(result
						.getColumnIndex(KEY_NAME)), result.getInt(result
						.getColumnIndex(KEY_CALORIES)), result.getFloat(result
						.getColumnIndex(KEY_PORC_CARBOHIDRATOS)), result
						.getFloat(result.getColumnIndex(KEY_PORC_LIPIDS)),
						result.getFloat(result
								.getColumnIndex(KEY_PORC_PROTEINS)), result
								.getInt(result.getColumnIndex(KEY_SUGAR)),
						result.getFloat(result.getColumnIndex(KEY_COLESTEROL))));
			} while (result.moveToNext());
		return devices;
	}

	public static void create(TransactionalFood food) {

	}

	public static TransactionalFood read(int idFood) {
		return null;

	}

	public static void update(TransactionalFood food) {

	}

	public static void delete(int idFood) {

	}
}
